db = DAL('sqlite://storage.sqlite')       # if not, use SQLite or other DB

from gluon.tools import *
auth=Auth(globals(),db)                      # authentication/authorization
auth.settings.hmac_key='sha512:f63994c0-36f3-46a0-8594-445e06de46e3'
auth.define_tables()                         # creates all needed tables
crud=Crud(globals(),db)                      # for CRUD helpers using auth
service=Service(globals())                   # for json, xml, jsonrpc, xmlrpc, amfrpc

user_id = auth.user.id if auth.is_logged_in() else 0
auth.user_id = user_id
login = A('login',_href=URL(r=request,f='user/login'))


db.define_table('jobs',
                Field('job_name','string'),
                Field('submitted_by',db.auth_user,default=user_id,writable=False,readable=False),
                Field('submitted_time','datetime',default=request.now,writable=False,readable=False),
                Field('job_id','string',writable=False,readable=False),
                Field('status','string',default='ready',writable=False,readable=False),
                Field('script','upload',autodelete=True),
                Field('stdout','upload',writable=False,readable=False,autodelete=True),
                Field('stderr','upload',writable=False,readable=False,autodelete=True)
)

db.jobs.script.requires=IS_NOT_EMPTY()
db.jobs.job_name.requires=IS_NOT_EMPTY()
db.jobs.status.requires=IS_IN_SET(('ready','running', 'complete'))

